package com.lhd.egweibo2.controller;

import com.lhd.egweibo2.mapper.UserMapper;
import com.lhd.egweibo2.pojo.dto.UserRegDTO;
import com.lhd.egweibo2.pojo.entity.User;
import com.lhd.egweibo2.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author lhd
 * @date 2024/4/24
 * @apiNote
 */
@Slf4j
@RestController
@RequestMapping(value ="/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping (value ="insert")
    public int reg(@RequestBody UserRegDTO userRegDTO){
        log.debug("", userRegDTO);
        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        if (userVO != null){
            return 2;
        }

        //用户名合法
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());

        log.debug("user={}",user);
        userMapper.insert(user);

        return 1;
    }
}
